/**
 * Copyright (c) Software Institude of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.jet.codegen;

import java.io.ByteArrayInputStream;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.codegen.jet.JETEmitter;

import edu.pku.sei.modeler.codegen.Activator;
import edu.pku.sei.modeler.jet.model.BundleInfo;
import edu.pku.sei.modeler.jet.model.DiagramInfo;
import edu.pku.sei.modeler.jet.util.ModelerConstants;

/**
 * @author zl
 *
 */
public class GenInternal extends AbstractGenerator {
	private IFolder folder;
	private List<DiagramInfo>dInfoList;
	private BundleInfo bInfo;
	
	public GenInternal() {
		
	}
	
	public void setDiagInfos(List<DiagramInfo> dInfoList) {
		this.dInfoList = dInfoList;
	}
	public void setBundleInfo(BundleInfo info) {
		this.bInfo = info;
	}
	/* (non-Javadoc)
	 * @see edu.pku.sei.modeler.jet.codegen.AbstractGenerator#generateCode(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public void generateCode(IProgressMonitor monitor)
			throws InterruptedException {
		assert proj != null;
		assert dInfoList != null;
		String codeBase = ModelerConstants.pkgBase + "." + ModelerConstants.internalBase;
		
		folder = proj.getFolder("src/"+codeBase.replace('.', '/'));
		try {
			folder.create(true, true, monitor);
		} catch (CoreException e) {
			e.printStackTrace();
			throw new InterruptedException("create internal folder failed");
		}
		genModelingProject(monitor);
		genModelRoot(monitor);
		genPaletteFactory(monitor);
		genProjectFactory(monitor);
		genStartupClass(monitor);
	}

	private translated.ModelProjGen genModelProj = translated.ModelProjGen.create("\n");
	public void genModelingProject(IProgressMonitor monitor)
		throws InterruptedException {
		try {
			/*String pluginId = Activator.getPluginId();
			String relativeUri = "templates/internal/ModelingProject.javajet";
			*/
			String projNature = "edu.pku.modeler.GeneratedNature";
			if(bInfo != null) {
				projNature = bInfo.getBundleID();
			}
			
			/*JETEmitter emitter = new JETEmitter(templatesBase + relativeUri, Activator.getDefault().getClass().getClassLoader());
			emitter.addVariable("JET_TUTORIAL", pluginId);
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.runtime");
			emitter.addVariable("EMF_ECORE", "org.eclipse.emf.ecore");
			emitter.addVariable("EMF_COMMON", "org.eclipse.emf.common");*/
			
			//String result = emitter.generate(monitor, new Object[]{projNature});
			
			String result = genModelProj.generate(projNature);
			
			IFile javaFile = folder.getFile(ModelerConstants.projClassName+".java");
			ByteArrayInputStream is = new ByteArrayInputStream(result.getBytes());
			javaFile.create(is, true, monitor);
		} catch(Exception e) {
			e.printStackTrace();
			throw new InterruptedException("generate modeling project failed");
		}
	}
	private translated.ModelRootGen genModelRoot = translated.ModelRootGen.create("\n");
	public void genModelRoot(IProgressMonitor monitor)
		throws InterruptedException {
		try {
			/*String pluginId = Activator.getPluginId();
			String relativeUri = "templates/internal/ModelRoot.javajet";
			
			JETEmitter emitter = new JETEmitter(templatesBase + relativeUri, Activator.getDefault().getClass().getClassLoader());
			emitter.addVariable("JET_TUTORIAL", pluginId);
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.runtime");
			emitter.addVariable("EMF_ECORE", "org.eclipse.emf.ecore");
			emitter.addVariable("EMF_COMMON", "org.eclipse.emf.common");
			*/
			//String result = emitter.generate(monitor, new Object[]{new Object()});
			
			String result = genModelRoot.generate(null);
			
			IFile javaFile = folder.getFile(ModelerConstants.modelRootName+".java");
			ByteArrayInputStream is = new ByteArrayInputStream(result.getBytes());
			javaFile.create(is, true, monitor);
		} catch(Exception e) {
			e.printStackTrace();
			throw new InterruptedException("generate model root failed");
		}
	}
	
	private translated.PaletteFactoryGen genPaletteFactory = translated.PaletteFactoryGen.create("\n");
	public void genPaletteFactory(IProgressMonitor monitor)
		throws InterruptedException {
		try {
	/*		String pluginId = Activator.getPluginId();
			String relativeUri = "templates/internal/PaletteFactory.javajet";
			
			JETEmitter emitter = new JETEmitter(templatesBase + relativeUri, Activator.getDefault().getClass().getClassLoader());
			emitter.addVariable("JET_TUTORIAL", pluginId);
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.runtime");
			emitter.addVariable("EMF_ECORE", "org.eclipse.emf.ecore");
			emitter.addVariable("EMF_COMMON", "org.eclipse.emf.common");
	*/		
			//String result = emitter.generate(monitor, new Object[]{dInfoList});
			
			String result = genPaletteFactory.generate(dInfoList);
			
			IFile javaFile = folder.getFile(ModelerConstants.paletteFactory+".java");
			ByteArrayInputStream is = new ByteArrayInputStream(result.getBytes());
			javaFile.create(is, true, monitor);
		} catch(Exception e) {
			e.printStackTrace();
			throw new InterruptedException("generate palette factory failed");
		}
	}
	
	private translated.ProjFactoryGen genProjFactory = translated.ProjFactoryGen.create("\n");
	public void genProjectFactory(IProgressMonitor monitor)
		throws InterruptedException {
		try {
/*			String pluginId = Activator.getPluginId();
			String relativeUri = "templates/internal/projectFactory.javajet";
			
			JETEmitter emitter = new JETEmitter(templatesBase + relativeUri, Activator.getDefault().getClass().getClassLoader());
			emitter.addVariable("JET_TUTORIAL", pluginId);
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.runtime");
			emitter.addVariable("EMF_ECORE", "org.eclipse.emf.ecore");
			emitter.addVariable("EMF_COMMON", "org.eclipse.emf.common");
*/			
//			String result = emitter.generate(monitor, new Object[]{new Object()});
			String result = genProjFactory.generate(null);
			IFile javaFile = folder.getFile(ModelerConstants.projFactory+".java");
			ByteArrayInputStream is = new ByteArrayInputStream(result.getBytes());
			javaFile.create(is, true, monitor);
		} catch(Exception e) {
			e.printStackTrace();
			throw new InterruptedException("generate project factory failed");
		}
	}
	
	private translated.StartUpGen genStartUp = translated.StartUpGen.create("\n");
	public void genStartupClass(IProgressMonitor monitor)
		throws InterruptedException {
		try {
/*			String pluginId = Activator.getPluginId();
			String relativeUri = "templates/internal/StartUp.javajet";
			
			JETEmitter emitter = new JETEmitter(templatesBase + relativeUri, Activator.getDefault().getClass().getClassLoader());
			emitter.addVariable("JET_TUTORIAL", pluginId);
			emitter.addVariable("JET_TUTORIAL", "edu.pku.sei.modeler.runtime");
			emitter.addVariable("EMF_ECORE", "org.eclipse.emf.ecore");
			emitter.addVariable("EMF_COMMON", "org.eclipse.emf.common");
*/			
//			String result = emitter.generate(monitor, new Object[]{new Object()});
			
			String result = genStartUp.generate(null);
			
			IFile javaFile = folder.getFile(ModelerConstants.starupName+".java");
			ByteArrayInputStream is = new ByteArrayInputStream(result.getBytes());
			javaFile.create(is, true, monitor);
		} catch(Exception e) {
			e.printStackTrace();
			throw new InterruptedException("generate startup class failed");
		}
	}
}
